/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.inmp;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.sinodata.bsm.common.message.BsmActiveMQFactory;
import com.sinodata.bsm.inmp.message.Messenger;
import com.sinodata.bsm.inmp.util.DBConnectionUtil;
import com.sinodata.bsm.inmp.util.SystemContext;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-8 下午5:03:48          tangli_ITSM        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public final class ProbeEnv {
    private final static Logger logger = Logger.getLogger(ProbeEnv.class);
    public static Long probeId = 1L;
    public static String probeName = "";
    public static String probeType = "";
    public static String probeIp = "localhost";

    public static void initConfig() throws Exception {
        logger.info("probe init...");
        Properties prop = new Properties();
        InputStreamReader isr = new InputStreamReader(new FileInputStream("inmp-conf/probe-inmp.properties"), "GBK");
        prop.load(isr);
        isr.close();
        probeId = Long.parseLong(prop.getProperty("probe.id"));
        probeName = prop.getProperty("probe.name");
        probeType = prop.getProperty("probe.type");
        probeIp = InetAddress.getLocalHost().getHostName();
    }

    public static void initEnv() throws Exception {
        PropertyConfigurator.configure("inmp-conf/log4j.properties");
        Properties prop = new Properties();
        InputStreamReader isr = new InputStreamReader(new FileInputStream("inmp-conf/probe-inmp.properties"), "GBK");
        prop.load(isr);
        isr.close();
        BsmActiveMQFactory.getInstance().init(prop);
        Messenger.getInstance().init(ProbeEnv.probeId);

        SystemContext.setProperty("ap_le_on", prop.getProperty("ap.le.on"));
        SystemContext.setProperty("ap_manager_on", prop.getProperty("ap.manager.on"));
        SystemContext.setProperty("ap_stor_on", prop.getProperty("ap.stor.on"));
        SystemContext.setProperty("gab_event_on", prop.getProperty("gab.event.on"));
        SystemContext.setProperty("gab_wsd_on", prop.get("gab.wsd.on"));
        SystemContext.setProperty("gab_anjian_on", prop.get("gab.anjian.on"));

        SystemContext.setProperty("poll_interval_indic", prop.getProperty("poll.interval.indic"));
        SystemContext.setProperty("poll_interval_event", prop.getProperty("poll.interval.event"));

        //集成朗易
        if ("true".equals(prop.getProperty("ap.le.on", "false").trim())) {
            //读socket端口
            SystemContext.setProperty("socket", prop.getProperty("probe.socket"));
            //初始化数据库
            String driver = prop.getProperty("le.jdbc.driver", "oracle.jdbc.driver.OracleDriver");
            String url = prop.getProperty("le.jdbc.url", "");
            String usr = prop.getProperty("le.jdbc.usr", "");
            String pwd = prop.getProperty("le.jdbc.psw", "");

            DBConnectionUtil.getInstance().initLeDBPool(driver, url, usr, pwd);

            //初始化laneasy的webservice地址
            String webs = prop.getProperty("le.http.port").trim();
            SystemContext.setProperty("wsurl", webs);
        }

        //集成zoho_op
        if ("true".equals(prop.getProperty("ap.manager.on", "false").trim())) {
            String driver = prop.getProperty("op.jdbc.driver", "org.postgresql.Driver");
            String url = prop.getProperty("op.jdbc.url", "");
            String usr = prop.getProperty("op.jdbc.usr", "");
            String pwd = prop.getProperty("op.jdbc.psw", "");
            DBConnectionUtil.getInstance().initManagerDBPool(driver, url, usr, pwd);

            SystemContext.setProperty("op_event_clr", prop.getProperty("op.event.clr"));
            SystemContext.setProperty("op_url", prop.getProperty("op.url"));
            SystemContext.setProperty("op_apikey", prop.getProperty("op.apikey"));

        }

        //集成opstor
        if ("true".equals(prop.getProperty("ap.stor.on", "false").trim())) {
            String driver = prop.getProperty("stor.jdbc.driver", "org.postgresql.Driver");
            String url = prop.getProperty("stor.jdbc.url", "");
            String usr = prop.getProperty("stor.jdbc.usr", "");
            String pwd = prop.getProperty("stor.jdbc.psw", "");

            DBConnectionUtil.getInstance().initStorDBPool(driver, url, usr, pwd);
        }

        //公安部事件类集成
        if ("true".equals(prop.getProperty("gab.event.on").trim())) {
            String driver = prop.getProperty("gab.jdbc.driver");
            String url = prop.getProperty("gab.jdbc.url");
            String usr = prop.getProperty("gab.jdbc.usr");
            String pwd = prop.getProperty("gab.jdbc.pwd");

            DBConnectionUtil.getInstance().initStorDBPool(driver, url, usr, pwd);
        }

        //公安部温湿度集成
        if ("true".equals(prop.getProperty("gab.wsd.on").trim())) {
            String driver = prop.getProperty("gab.wsd.jdbc.driver");
            String url = prop.getProperty("gab.wsd.jdbc.url");

            DBConnectionUtil.getInstance().initGabDBPool(driver, url, "", "");
        }

        //安监系统集成
        if ("true".equals(prop.getProperty("gab.anjian.on").trim())) {
            String driver = prop.getProperty("gab.anjian.jdbc.driver");
            String url = prop.getProperty("gab.anjian.jdbc.url");
            String user = prop.getProperty("gab.anjian.jdbc.user");
            String pwd = prop.getProperty("gab.anjian.jdbc.pwd");
            DBConnectionUtil.getInstance().initanjianDBPool(driver, url, user, pwd);
        }
        //quartz
        Properties quartz = new Properties();
        InputStreamReader tsr = new InputStreamReader(new FileInputStream("inmp-conf/quartz.properties"), "GBK");
        quartz.load(tsr);
        tsr.close();
        SystemContext.setProperty("quatrz.thread", quartz.getProperty("org.quartz.threadPool.threadCount"));
    }

}
